
package com.yimixian.app.ui.wheelview;

import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.yimixian.app.EventBusDomain.CommentMessage;
import com.yimixian.app.R;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import de.greenrobot.event.EventBus;

/**
 * Created by songzy on 2015/10/27.
 */
public class CommentDialog {
    private Context mContext;
    public AlertDialog mDialog;
    private EditText mCommentContent;
    private RatingBar tasteRating;
    private RatingBar freshnessRating;
    private Button commitButton;
    private ImageView cancelButton;

    private int mGoodId;
    private View mCommitButton;

    public CommentDialog(Context context) {
        this.mContext = context;
    }

    private AlertDialog initDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
        View view = View.inflate(mContext, R.layout.dialog_comment, null);
        TextView ratingBarText1 = (TextView) view.findViewById(R.id.taste);
        TextView ratingBarText2 = (TextView) view.findViewById(R.id.freshness);
        if(TextUtils.isEmpty(SharedPreferencesHelper.getRatingBarText(SharePreferenceKeys.RATINGBAR_TEXT1))) {
            ratingBarText1.setText("口感:");
        } else {
            ratingBarText1.setText(SharedPreferencesHelper.getRatingBarText(SharePreferenceKeys.RATINGBAR_TEXT1) + ":");
        }
        if(TextUtils.isEmpty(SharedPreferencesHelper.getRatingBarText(SharePreferenceKeys.RATINGBAR_TEXT2))) {
            ratingBarText2.setText("推荐度:");
        } else {
            ratingBarText2.setText(SharedPreferencesHelper.getRatingBarText(SharePreferenceKeys.RATINGBAR_TEXT2) + ":");
        }
        mCommentContent = (EditText) view.findViewById(R.id.edit_comment_content);
        tasteRating = (RatingBar) view.findViewById(R.id.ratingbar_1);
        freshnessRating = (RatingBar) view.findViewById(R.id.ratingbar_2);
        commitButton = (Button) view.findViewById(R.id.comment_commit);
        commitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommentMessage message = new CommentMessage();
                if (tasteRating != null && freshnessRating != null) {
                    if ((int) Math.ceil(tasteRating.getRating()) == 0
                            || (int) Math.ceil(freshnessRating.getRating()) == 0) {
                        UiUtils.showToast("请给商品打分");
                        return;
                    } else if (mCommentContent.getText().length() < 1) {
                        UiUtils.showToast("至少填写1个字符");
                        return ;
                    } else if (mCommentContent.getText().length() > 120) {
                        UiUtils.showToast("至多填写120个字符");
                        return ;
                    }
                    message.taste = (int) Math.ceil(tasteRating.getRating());
                    message.freshness = (int) Math.ceil(freshnessRating.getRating());
                }
                if (mCommentContent != null) {
                    message.comment = mCommentContent.getText().toString().trim();
                }
                message.button = mCommitButton;
                message.goodId = mGoodId;
                message.button = mCommitButton;
                EventBus.getDefault().post(message);
                hideDialog();
            }
        });
        cancelButton = (ImageView) view.findViewById(R.id.comment_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideDialog();
            }
        });
        AlertDialog dialog = dialogBuilder.create();
        dialog.setView(view);
        return dialog;
    }

    public void setGoodId(int goodId) {
        this.mGoodId = goodId;
    }

    public void setButton(View button) {
        this.mCommitButton = button;
    }

    public void showDialog() {
        if (mContext == null) {
            return;
        }
        if (mDialog == null) {
            mDialog = initDialog();
        }
        mDialog.show();
    }

    public void hideDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
}
